package com.java.controller.center;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.java.common.utils.IMOOCJSONResult;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.pojo.Orders;
import com.java.pojo.vo.OrderStatusCountsVO;
import com.java.service.center.MyOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * @author jl
 * Created on 2020/10/19
 */
@Api(tags = "用户订单管理")
@RestController
@RequestMapping("/myorders")
@AllArgsConstructor
public class MyOrderController {
    private final MyOrdersService ordersService;

    @ApiOperation(value = "查询订单动向", notes = "查询订单动向", httpMethod = "POST")
    @PostMapping("/trend")
    public R trend(@RequestParam String userId, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page, @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageUtils pageUtils = ordersService.getOrdersTrend(userId, page, pageSize);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "获得订单状态数概况", notes = "获得订单状态数概况", httpMethod = "POST")
    @PostMapping("/statusCounts")
    public R statusCounts(@RequestParam String userId) {
        OrderStatusCountsVO result = ordersService.getOrderStatusCounts(userId);
        return R.ok(result);
    }

    @ApiOperation("搜索用户订单列表")
    @PostMapping("/query")
    public R search(@RequestParam(value = "userId") String userId, @RequestParam(value = "orderStatus", required = false) Integer status,
                    @RequestParam(value = "page", required = false, defaultValue = "1") Integer page, @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageUtils pages = ordersService.queryMyOrders(userId, status, page, pageSize);
        return R.ok(pages);
    }

    // 商家发货没有后端，所以这个接口仅仅只是用于模拟
    @ApiOperation(value = "商家发货", notes = "商家发货", httpMethod = "GET")
    @GetMapping("/deliver")
    public R deliver(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId) throws Exception {

        if (StringUtils.isBlank(orderId)) {
            return R.error("订单ID不能为空");
        }
        ordersService.updateDeliverOrderStatus(orderId);
        return R.ok();
    }

    @ApiOperation(value = "用户确认收货", notes = "用户确认收货", httpMethod = "POST")
    @PostMapping("/confirmReceive")
    public R confirmReceive(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) {
        R checkResult = checkUserOrder(userId, orderId);
        if (Integer.parseInt(checkResult.get("status").toString()) != HttpStatus.OK.value()) {
            return checkResult;
        }

        boolean res = ordersService.updateReceiveOrderStatus(orderId);
        if (!res) {
            return R.error("订单确认收货失败！");
        }

        return R.ok();
    }

    @ApiOperation(value = "用户删除订单", notes = "用户删除订单", httpMethod = "POST")
    @PostMapping("/delete")
    public R delete(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) {

        R checkResult = checkUserOrder(userId, orderId);
        if (Integer.parseInt(checkResult.get("status").toString()) != HttpStatus.OK.value()) {
            return checkResult;
        }

        boolean res = ordersService.deleteOrder(userId, orderId);
        if (!res) {
            return R.error("订单删除失败！");
        }

        return R.ok();
    }

    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用
     */
    public R checkUserOrder(String userId, String orderId) {
        Orders order = ordersService.getOne(new LambdaQueryWrapper<Orders>().eq(Orders::getUserId, userId).eq(Orders::getId, orderId));
        if (order == null) {
            return R.error("订单不存在！");
        }
        return R.ok(order);
    }
}
